//
//  YJWRecommendTagController.m
//  百思不得骑姐
//
//  Created by YJW on 16/10/28.
//  Copyright © 2016年 yijiang. All rights reserved.
//

#import "YJWRecommendTagController.h"
#import "YJWRecommendTagsModel.h"
#import "YJWRecommendTagCell.h"

#import <AFNetworking.h>
#import <MJExtension.h>
#import <SVProgressHUD.h>

@interface YJWRecommendTagController ()

/** 数据   */
@property(nonatomic,strong) NSArray *tags;

@end

@implementation YJWRecommendTagController

static NSString *const reuseId = @"tag";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = YJWBgColor;
    
    self.title = @"推荐标签";
    
    [self setUpTableView];
    
    [self loadData];
}

//设置 tableView
-(void)setUpTableView
{
    //注册 cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YJWRecommendTagCell class]) bundle:nil] forCellReuseIdentifier:@"tag"];
    
    self.tableView.rowHeight = 60;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

//加载数据
-(void)loadData
{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];

    NSMutableDictionary *paras = [NSMutableDictionary dictionary];
    paras[@"a"] = @"tag_recommend";
    paras[@"action"] = @"sub";
    paras[@"c"] = @"topic";
    
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:paras progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.tags = [YJWRecommendTagsModel mj_objectArrayWithKeyValuesArray:responseObject];
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.tags.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YJWRecommendTagCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    
    cell.recommendTag = self.tags[indexPath.row];
    return cell;
}
@end
